﻿using System.Data;
using OVS.CoreDomain;
using OVS.CoreDomain.Data;
using System.Collections.Generic;
using System;

namespace OVS.Data
{
    public class DalVoter : Dal<IVoter, Voter>, IDalVoter
    {

        private const string SP_GET_VOTER = "usp_GetVoter";
        private const string SP_ADD_VOTER = "usp_InsertVoter";
        private const string SP_UPDATE_VOTER = "usp_UpdateVoter";

        private const string SP_SEARCH_VOTERS = "usp_searchVoters";
        private const string SP_VOTER_HAVE_VOTED = "usp_voterHaveVoted";
        private const string SP_UPDATE_PASSWORD = "usp_UpdatePassword";



         private readonly IDalManager _manager;
        private static readonly IDalVoter _instance = new DalVoter();

        public static IDalVoter Instance
        {
            get { return _instance; }
        }

        public DalVoter()
        {
            _manager = new DalManager();
        }

        public DalVoter(IDalManager manager)
        {
            _manager = manager;
        }

        protected override void Convert(IVoter obj, DataRow row)
        {
            obj.VoterId = _manager.GetValue<string>(row, "VoterId");
            obj.Name = _manager.GetValue<string>(row, "Name");
            obj.FatherName = _manager.GetValue<string>(row, "FatherName");
            obj.Address = _manager.GetValue<string>(row, "Address");
            obj.EmailId = _manager.GetValue<string>(row, "EmailId");
            obj.DateOfBirth = _manager.GetValue<DateTime>(row, "DateOfBirth");
            obj.WardId = _manager.GetValue<string>(row, "WardId");
            obj.DistrictId = _manager.GetValue<string>(row, "DistrictId");
            obj.PanchayatId = _manager.GetValue<string>(row, "PanchayatId");
            obj.HaveVoted = _manager.GetValue<bool>(row, "HaveVoted");
            obj.Password = _manager.GetValue<string >(row, "Password");

        }

        public IVoter GetVoter(string voterId)
        {
            _manager.StoredProcedureName = SP_GET_VOTER;
            _manager.AddInParameter("@VoterId", DbType.String, voterId);
            IVoter voter = Convert(_manager.ExecuteDataSet());
            return voter;
        }

        public IList<IVoter> SearchVoters(IVoterSearchCriteria searchCriteria)
        {
            _manager.StoredProcedureName = SP_SEARCH_VOTERS;
            _manager.AddInParameter("@DistrictId", DbType.String, searchCriteria.DistrictId);
            _manager.AddInParameter("@PanchayatId", DbType.String, searchCriteria.PanchayatId);
            _manager.AddInParameter("@WardId", DbType.String, searchCriteria.WardId);
            _manager.AddInParameter("@Name", DbType.String, searchCriteria.Name);
            _manager.AddInParameter("@FatherName", DbType.String, searchCriteria.FatherName);
            _manager.AddInParameter("@VoterId", DbType.String, searchCriteria.VoterId);

            IList<IVoter> listVoters = new List<IVoter>();
            foreach (IVoter voter in ConvertAll(_manager.ExecuteDataSet()))
                listVoters.Add(voter);

            return listVoters;

        }
        public bool AddVoter(IVoter voter)
        {
            _manager.StoredProcedureName = SP_ADD_VOTER;

            _manager.AddInParameter("@DistrictId", DbType.String, voter.DistrictId);
            _manager.AddInParameter("@PanchayatId", DbType.String, voter.PanchayatId );
            _manager.AddInParameter("@WardId", DbType.String, voter.WardId);
            _manager.AddInParameter("@VoterId", DbType.String, voter.VoterId);
            _manager.AddInParameter("@Name", DbType.String, voter.Name);
            _manager.AddInParameter("@FatherName", DbType.String, voter.FatherName );
            _manager.AddInParameter("@Address", DbType.String, voter.Address );
            _manager.AddInParameter("@EmailId", DbType.String, voter.EmailId );
            _manager.AddInParameter("@DateOfBirth", DbType.DateTime, voter.DateOfBirth );
            _manager.AddInParameter("@Password", DbType.String, voter.Password);

            
            
           // _manager.AddParameter("@binaryValue", SqlDbType.VarBinary, voter.Photo.Length).Value = voter.Photo;
           // _manager.AddInParameter("@Photo", DbType.B , voter.Photo);
            

            _manager.ExecuteNonQuery();

            return true;

        }

        public bool UpdateVoter(IVoter voter)
        {
            _manager.StoredProcedureName = SP_UPDATE_VOTER;

            _manager.AddInParameter("@DistrictId", DbType.String, voter.DistrictId);
            _manager.AddInParameter("@PanchayatId", DbType.String, voter.PanchayatId);
            _manager.AddInParameter("@WardId", DbType.String, voter.WardId);
            _manager.AddInParameter("@VoterId", DbType.String, voter.VoterId);
            _manager.AddInParameter("@Name", DbType.String, voter.Name);
            _manager.AddInParameter("@FatherName", DbType.String, voter.FatherName);
            _manager.AddInParameter("@Address", DbType.String, voter.Address);
            _manager.AddInParameter("@EmailId", DbType.String, voter.EmailId);
            _manager.AddInParameter("@DateOfBirth", DbType.DateTime, voter.DateOfBirth);
            _manager.ExecuteNonQuery();

            return true;

        }

        #region IDalVoter Members


        public IVoter HaveVoted(IElection election, IVoter voter)
        {
            _manager.StoredProcedureName = SP_VOTER_HAVE_VOTED;
            _manager.AddInParameter("@VoterId", DbType.String, voter.VoterId);
            _manager.AddInParameter("@ElectionId", DbType.String, election.ElectionId);
            IVoter _voter = Convert(_manager.ExecuteDataSet());
            return _voter;
        }

        #endregion

        #region IDalVoter Members


        public bool UpdatePassword(IVoter voter)
        {
            _manager.StoredProcedureName = SP_UPDATE_PASSWORD;

          
            _manager.AddInParameter("@VoterId", DbType.String, voter.VoterId);
            _manager.AddInParameter("@Password", DbType.String, voter.Password );
           
            _manager.ExecuteNonQuery();

            return true;
        }

        #endregion
    }
}
